package com.cnfangmao.masterdata.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.common.util.ModelUtils;
import com.cnfangmao.masterdata.common.util.SnowFlakeUtils;
import com.cnfangmao.masterdata.dto.AccountBasicDataRightDto;
import com.cnfangmao.masterdata.dto.AccountBasicDataRightSearchDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.AccountBasicDataRight;
import com.cnfangmao.masterdata.service.AccountBasicDataRightService;
import com.cnfangmao.masterdata.vo.GeneralPageVo;

/**
 * <p>
 * 用户主数据权限表 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@RequestMapping("/accountBasicDataRight")
@Api(tags="1-5、配置-用户主数据权限管理")
@RestController
public class AccountBasicDataRightController {

	private static final Logger logger = LoggerFactory.getLogger(AccountBasicDataRightController.class);

	@Autowired
	private AccountBasicDataRightService accountBasicDataRightService;

	@PostMapping(value="/list.do")
	@ApiOperation(value="用户主数据权限列表")
	@ApiImplicitParam(name="dto",value="用户主数据权限查询条件",required=true, paramType = "body",dataType="GeneralSearchDto«AccountBasicDataRightSearchDto»")
	private ResBody<GeneralPageVo<AccountBasicDataRight>> listAccountBasicDataRight(@RequestBody GeneralSearchDto<AccountBasicDataRightSearchDto> dto) {
		ResBody<GeneralPageVo<AccountBasicDataRight>> resBody = new ResBody<GeneralPageVo<AccountBasicDataRight>>();
		try {
			IPage<AccountBasicDataRight>  accountBasicDataRight =accountBasicDataRightService.queryListAccountBasicDataRight(dto);
			GeneralPageVo<AccountBasicDataRight> result= new GeneralPageVo<AccountBasicDataRight>();
			result.setPageNo(accountBasicDataRight.getCurrent());
			result.setPageSize(accountBasicDataRight.getPages());
			result.setRecords(accountBasicDataRight.getRecords());
			result.setTotalCount(accountBasicDataRight.getTotal());
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(result);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run list error in AccountBasicDataRightController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}

	@PostMapping(value="/create.do")
	@ApiOperation(value="创建用户主数据权限信息")
	@ApiImplicitParam(name="dto",value="创建用户主数据权限信息",required=true,paramType="body",dataType="AccountBasicDataRightDto")
	public ResBody<Boolean> createAccountBasicDataRight(@RequestBody AccountBasicDataRightDto dto) {
		ResBody<Boolean> resBody =new ResBody<Boolean>();
		try {
			if (dto.getUserId().isEmpty() || dto.getMetadataId().isEmpty()) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("用户Id或者主数据元数据Id为空");
				return resBody;
			}
			if(accountBasicDataRightService.findByUserIdAndMetadataId(dto.getUserId(), dto.getMetadataId())!=null)
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("用户主数据权限已经存在");
				return resBody;			
			}
			dto.setRightId(SnowFlakeUtils.generator());
			AccountBasicDataRight accountBasicDataRight =ModelUtils.parseObject(dto, AccountBasicDataRight.class,"create");
			accountBasicDataRightService.save(accountBasicDataRight);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("创建成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run create error in AccountBasicDataRightController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;	
	}
	
	@PostMapping(value="/update.do")
	@ApiOperation(value="修改用户主数据权限信息")
	@ApiImplicitParam(name="dto",value="修改用户主数据权限信息",required=true,paramType="body",dataType="AccountBasicDataRightDto")
	public ResBody<Boolean> updateAccountBasicDataRight(@RequestBody AccountBasicDataRightDto dto) {
		ResBody<Boolean>  resBody =new ResBody<Boolean>();
		try {
			if (dto.getUserId().isEmpty() || dto.getMetadataId().isEmpty()) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("用户Id或者主数据元数据Id为空");
				return resBody;
			}
			AccountBasicDataRight accountBasicDataRight=accountBasicDataRightService.findByUserIdAndMetadataId(dto.getUserId(), dto.getMetadataId());
			if(accountBasicDataRight!=null && !accountBasicDataRight.getRightId().equals(dto.getRightId()))
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("用户主数据权限已经存在");
				return resBody;			
			}
			AccountBasicDataRight entity =ModelUtils.parseObject(dto, AccountBasicDataRight.class,"update");
			accountBasicDataRightService.updateById(entity);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("修改成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run update error in AccountBasicDataRightController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
		
	}
	
	@PostMapping(value="/delete.do")
	@ApiOperation(value="删除用户主数据权限信息")
	@ApiImplicitParam(name="rightId",value="删除用户主数据权限信息 rightId",required=true,paramType="body",dataType="string")
	public ResBody<Boolean>  deleteAccountBasicDataRight(@RequestBody String rightId) {
		ResBody<Boolean>  resBody =new ResBody<Boolean>();
		try {
			accountBasicDataRightService.removeById(rightId);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("删除成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run delete error in AccountBasicDataRightController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	@PostMapping(value="/getAccountBasicDataRight.do")
	@ApiOperation(value="获取用户主数据权限信息")
	@ApiImplicitParam(name="rightId",value="获取用户主数据权限信息 rightId",required=true,paramType="body",dataType="string")
	public ResBody<AccountBasicDataRight>  getAccountBasicDataRight(@RequestBody String rightId) {
		ResBody<AccountBasicDataRight>  resBody = new ResBody<AccountBasicDataRight>();
		try {
			AccountBasicDataRight accountBasicDataRight=accountBasicDataRightService.getById(rightId);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(accountBasicDataRight);
		} catch (Exception e) {
			logger.error(String.format("run getAccountBasicDataRight error in AccountBasicDataRightController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;	
	}
}
